package Dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Demo2 extends JDialog {

	private final JPanel contentPanel = new JPanel();
	JTextArea textArea = new JTextArea();

	void UI(String s) {
		try {
			for (int i = 0; i < 100; i++) {
				Thread.sleep(1000);
				SwingUtilities.invokeLater(new UpdateTextArea(s + i));
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Demo2 dialog = new Demo2();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class UpdateUI extends Thread {
		String s;

		public UpdateUI(String s) {
			this.s = s;
		}

		public void run() {
			//UI(s);
			try {
				for (int i = 0; i < 50; i++) {
					Thread.sleep(1000);
					//SwingUtilities.invokeLater(new UpdateTextArea(s + i));
					textArea.append(s +i+ "\n");
					textArea.validate();
					textArea.repaint();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	class UpdateTextArea implements Runnable {
		String s;

		public UpdateTextArea(String s) {
			this.s = s;
		}

		public void run() {
			textArea.append(s + "\n");
			textArea.validate();
			textArea.repaint();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Demo2() {
		setBounds(100, 100, 498, 341);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		textArea.setEditable(false);
		textArea.setText("Added   setting to control the behaviour when previewing");
		JScrollPane scrollPane = new JScrollPane(textArea);
		scrollPane.setBounds(24, 20, 435, 240);
		contentPanel.add(scrollPane);
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Thread d = new UpdateUI("ddddddddddddddddd");
				d.start();
			}
		});
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);
	}

}
